package service;

import dao.IUserDao;
import dao.UserDaoImpl;
import entity.User;

import java.util.Date;
import java.util.List;

public class UserServiceImpl implements IUserService {
    private IUserDao userDao = new UserDaoImpl();

    @Override
    public boolean addUser(User user) {
        User existingUser = userDao.getUserByUsername(user.getUsername());
        if (existingUser != null) {
            return false;
        }
        for (User u : userDao.getAllUsers()) {
            if (u.getPhone().equals(user.getPhone())) {
                return false;
            }
        }
        user.setPassword("123456");
        user.setRegisterTime(new Date());
        return userDao.addUser(user);
    }

    @Override
    public boolean deleteUser(String userId) {
        return userDao.deleteUser(userId);
    }

    @Override
    public boolean updateUser(User user) {
        return userDao.updateUser(user);
    }

    @Override
    public User getUserById(String userId) {
        return userDao.getUserById(userId);
    }

    @Override
    public User getUserByUsername(String username) {
        return userDao.getUserByUsername(username);
    }

    @Override
    public List<User> getAllUsers() {
        return userDao.getAllUsers();
    }

    @Override
    public User login(String username, String password) {
        return userDao.login(username, password);
    }

    @Override
    public boolean resetPassword(String userId, String newPassword) {
        User user = userDao.getUserById(userId);
        if (user != null) {
            user.setPassword(newPassword);
            return userDao.updateUser(user);
        }
        return false;
    }
}